package com.ccjq.jq173.core.util;

import java.sql.*;

/**
 * 数据库连接工具类
 */
public class DbConnection {
    private  static  final  String DRIVER;
    private  static  final  String URL;
    private  static  final  String USERNAME;
    private  static  final  String PASSWORD;
    static{
        DbConfigReader dcr=new DbConfigReader();
        DRIVER=dcr.getValue("db.driver");
        URL=dcr.getValue("db.url");
        USERNAME=dcr.getValue("db.username");
        PASSWORD=dcr.getValue("db.password");
     }

    /**
     * 建立数据库连接
     * @return 返回连接对象
     */
     public static Connection getConnection(){
        Connection con=null;
         try {
             Class.forName(DRIVER);
             con= DriverManager.getConnection(URL,USERNAME,PASSWORD);
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return con;
     }

    /**
     *  关闭数据库相关资源
     * @param rs 结果集关闭
     * @param st 语句对象
     * @param conn 连接对象
     */
     public static void close(ResultSet rs, Statement st, Connection conn){
         try {
            if(rs!=null){
                rs.close();
            }
            if(st!=null){
                st.close();
            }
            if(conn!=null){
                conn.close();
            }
         }catch (SQLException e) {
             e.printStackTrace();
         }
     }
}
